package com.nf.xiaomi.service.impl;

import com.nf.xiaomi.dao.OrderDao;
import com.nf.xiaomi.dao.ProductDao;
import com.nf.xiaomi.dao.ProductInfoDao;
import com.nf.xiaomi.dao.UserDao;
import com.nf.xiaomi.dao.impl.OrderDaoImpl;
import com.nf.xiaomi.dao.impl.ProductDaoImpl;
import com.nf.xiaomi.dao.impl.ProductInfoDaoImpl;
import com.nf.xiaomi.dao.impl.UserDaoImpl;
import com.nf.xiaomi.entity.OrderEntity;
import com.nf.xiaomi.entity.ProductEntity;
import com.nf.xiaomi.entity.UsersEntity;
import com.nf.xiaomi.model.AttributeModel;
import com.nf.xiaomi.service.OrderService;
import com.nf.xiaomi.util.JsonUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderServiceImpl implements OrderService {
    OrderDao orderDao = new OrderDaoImpl();
    UserDao userDao = new UserDaoImpl();


    @Override
    public List<OrderEntity> getAll() {
        List<OrderEntity> orderDaoAll = orderDao.getAll();
        return orderDaoAll;
    }

    @Override
    public List<OrderEntity> getAll(Integer orderState) {
        List<OrderEntity> orderDaoAll = orderDao.getAll(orderState);
        return orderDaoAll;
    }

    public List<OrderEntity> getAll(Long account) {
        List<OrderEntity> orderDaoAll = orderDao.getAll(account);
        return orderDaoAll;
    }

    public Integer insert(OrderEntity order) {
        List<UsersEntity> usersEntities = userDao.getAll(order.getUserAccount());
        for (UsersEntity usersEntity : usersEntities) {
            order.setUser_id(usersEntity.getUserID());
        }
        return orderDao.insert(order);
    }

    public Integer update(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        Integer orderID = Integer.valueOf(request.getParameter("out_trade_no"));
        String passbackParams = request.getParameter("passback_params");
        String decodedParams = URLDecoder.decode(passbackParams, "UTF-8");
        // 拆分键值对
        Map<String, String> paramMap = new HashMap<>();
        String[] keyValuePairs = decodedParams.split(",");
        for (String pair : keyValuePairs) {
            String[] keyValue = pair.split(":");
            if (keyValue.length == 2) {
                String key = keyValue[0];
                String value = keyValue[1];
                paramMap.put(key, value);
            }
        }
        Long account = Long.valueOf(paramMap.get("account"));
        Integer state = Integer.valueOf(paramMap.get("state"));
        Integer userID = getUserID(account);
        return orderDao.update(userID,orderID,state);
    }

    public Integer deleteByID(Long account, Integer orderID) {
        Integer userID = getUserID(account);
        return orderDao.deleteByID(userID,orderID);
    }

    @Override
    public List<OrderEntity> total() {
        return orderDao.total();
    }

    public List<OrderEntity> confirmOrder(Long account, Integer orderID) {
        List<OrderEntity> orderDaoAll = orderDao.confirmOrder(account, orderID);
        return orderDaoAll;
    }

    public Long paymentCount(Long account) {
        return orderDao.paymentCount(getUserID(account));
    }

    public Long goodsCount(Long account) {
        return orderDao.goodsCount(getUserID(account));
    }

    public Long recycleCount(Long account) {
        return orderDao.recycleCount(getUserID(account));
    }

    private Integer getUserID(Long account) {
        List<UsersEntity> usersEntities = userDao.getAll(account);
        Integer userID = 0;
        for (UsersEntity usersEntity : usersEntities) {
            userID = usersEntity.getUserID();
        }
        return userID;
    }
}
